<?php

namespace App\Http\Controllers\Admin;


use Illuminate\Http\Response;
use App\Models\Admin\Apage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class ApageController extends Controller
{
    /**
     * @var UserRepository
     */
    private $apage;


    /**
     * ApageController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->apage = new Apage();
    }

    /**
     * 单页列表
     *
     * @return Response
     */
    public function index()
    {
        $apageObj = $this->apage->orderBy('apage_id', 'desc')->paginate(15, ['*'], 'pageNum');

        return view('admin.Apage.index', ['apage' => $apageObj]);
    }

    /**
     * 创建单页
     *
     * @return Response
     */
    public function create(Request $request)
    {
        if ($request->getMethod() == "POST") {

            $validator = Validator::make($request->all(),
                [
                    'apage_title' => 'required|min:2|max:255',
                    'apage_content' => 'required',
                ],
                [
                    'apage_title.required' => '单页标题不能为空',
                    'apage_title.min' => '单页标题内容不能小于2个汉字',
                    'apage_title.max' => '单页标题内容不能大于100个汉字',
                    'apage_content.required' => '单页内容不能为空',
                ]
            );

            if ($validator->fails()) {
                return view('admin.Apage.create')->withErrors($validator);
            }
            $insert = $this->apage;

            $insert->apage_title = $request->input('apage_title');
            $insert->apage_abstract = $request->input('apage_abstract');
            $insert->apage_content = $request->input('apage_content');
            $insert->apage_image = $request->input('apage_image');
            $insert->apage_keyword = $request->input('apage_keyword');
            $insert->apage_publish_time = strtotime($request->input('apage_publish_time'));
            $insert->apage_views = $request->input('apage_views');
            $insert->apage_state = $request->input('apage_state');

            if ($insert->save()) {
                return redirect()->route('admin.Apage')->with('success','创建成功!');
            } else {
                return view('admin.Apage.create')->withErrors($insert->save());
            }

        }


        return view('admin.Apage.create');


    }


    /**
     * 编辑单页
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit(Request $request,$id)
    {
        $apageObj=$this->apage->find($id);
        if (!$apageObj) {
            $errors='内容不存在！';
            return redirect()->route('admin.Apage')->withErrors(compact('errors'));
        }

        if($request->getMethod()=="POST"){

            $apageObj->apage_title = $request->input('apage_title');
            $apageObj->apage_abstract = $request->input('apage_abstract');
            $apageObj->apage_content = $request->input('apage_content');
            $apageObj->apage_image = $request->input('apage_image');
            $apageObj->apage_keyword = $request->input('apage_keyword');
            $apageObj->apage_publish_time = strtotime($request->input('apage_publish_time'));
            $apageObj->apage_views = $request->input('apage_views');
            $apageObj->apage_state = $request->input('apage_state');
            $apageObj->update();

            return redirect()->route('admin.Apage')->with('success','保存成功!');

        }
        return view('admin.Apage.edit', ['apage'=>$apageObj]);
    }


    /**
     * 删除单页
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $apageObj=$this->apage->find($id);
        if (!$apageObj) {
            $errors='内容不存在！';
            return redirect()->route('admin.Apage')->withErrors(compact('errors'));
        }
        $apageObj->delete($id);

        return redirect()->route('admin.Apage')->with('success','删除成功!');
    }


}
